package com.xlx.openLink.uitls;

import com.xlx.openLink.conf.MailBean;
import com.xlx.openLink.conf.MysqlProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.BlockingDeque;

@Slf4j
public class Commuitls {

    @Autowired
    private MysqlProperties mysqlProperties;

    public static boolean ping(String ipAddress) {
        boolean status = false;   // 当返回值是true时，说明host是可用的
        try {
            status = InetAddress.getByName(ipAddress).isReachable(100);
        } catch (IOException e) {
            log.error(e.toString());
            e.printStackTrace();
        }
        return status;
    }

    public static double formatDouble(double d) {
        DecimalFormat df = new DecimalFormat("#.00");
        return Double.valueOf(df.format(d));
    }


    public static double doubleCalc(long l1, long l2, int x) {
        BigDecimal bigDecimal = new BigDecimal(0);
        if (l1 - l2 > 0) {
            bigDecimal = new BigDecimal(l1 - l2);
            log.info("value1-value2 ---- :" + (l1 - l2));
        } else if (l1 - l2 == 0) {
            bigDecimal = new BigDecimal(0);
            log.info("value1----: " + l1 + "   :value2----" + l2);
        } else {
            bigDecimal = new BigDecimal(l1);
            log.info("value1---:" + l1 + "   :value2----" + l2);
        }
        log.info("时间是:" + x + "ms");
        BigDecimal time = new BigDecimal(x);

        double d = bigDecimal.divide(time.divide(new BigDecimal(1000),4,RoundingMode.HALF_UP), 4, RoundingMode.HALF_UP).divide(BigDecimal.valueOf(1024 * 1024 * 8), 4, RoundingMode.HALF_UP).doubleValue();
        return d;

    }


    public static List<List> spList(List list) {
        List<List> lists = new ArrayList<>();
        int loopCount = list.size() / 10;
        int left = list.size() % 10;
        for (int i = 0; i < loopCount; i++) {
            int fromIndex = i * 10;
            lists.add(list.subList(fromIndex, fromIndex + 10));
        }
        if (left > 0) {
            int fromIndex = loopCount * 10;
            lists.add(list.subList(fromIndex, fromIndex + left));
        }
        return lists;
    }

    public static List<List> spList(List list, int num) {
        List<List> lists = new ArrayList<>();
        int loopCount = list.size() / num;
        int left = list.size() % num;
        for (int i = 0; i < loopCount; i++) {
            int fromIndex = i * num;
            lists.add(list.subList(fromIndex, fromIndex + num));
        }
        if (left > 0) {
            int fromIndex = loopCount * num;
            lists.add(list.subList(fromIndex, fromIndex + left));
        }
        return lists;
    }


    public static List que2List(BlockingDeque blockingDeque) {
        List list = new ArrayList();
        Iterator iterator = blockingDeque.iterator();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }

    public static void sendEmail(JavaMailSender jms, String to, String con, String date) {
        //建立邮件消息
        SimpleMailMessage mainMessage = new SimpleMailMessage();
        //发送者
        mainMessage.setFrom("15682127007@163.com");
        //接收者
        mainMessage.setTo(to);
        //发送的标题
        mainMessage.setSubject("连发通信智慧管控平台消息");
        //发送的内容
        String reCon = con + "\n" + "告警产生时间:" + date;
        mainMessage.setText(reCon);
        jms.send(mainMessage);
    }


}


